# 1.导包
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

# 2.创建浏览器驱动对象
# Firefox浏览器:
# driver = webdriver.Firefox()
# Chrome浏览器:
path = r"D:\Programs\Python\Python313\chromedriver.exe"
ser = Service(executable_path = path)
driver = webdriver.Chrome(service = ser)
# 使用Edge浏览器,不需要安装驱动
# driver = webdriver.Edge ()
time.sleep(3)
# 3.打开Web页面
driver.get("http://hmshop-test.itheima.net/")
driver.maximize_window()
# 4.页面操作,tpshop的登录
WebDriverWait(driver,10).until(lambda x: x.find_element(by=By.LINK_TEXT, value="登录"))
# 4.1 进入登录页面
driver.find_element(by=By.LINK_TEXT, value="登录").click()
# 4.2 输入信息
driver.find_element(by=By.ID, value="username").send_keys("13400000011")
driver.find_element(by=By.ID, value="password").send_keys("123456")
driver.find_element(by=By.ID, value="verify_code").send_keys("8888")
driver.find_element(by=By.LINK_TEXT, value="登    录").click()
WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.CSS_SELECTOR, value=".u-dt"))
action = ActionChains(driver)
action.move_to_element(driver.find_element(by=By.CSS_SELECTOR, value=".u-dt"))
action.perform()
# driver.find_element(by=By.XPATH, value="/html/body/div[2]/div/div[3]/ul/li[2]/div/div[2]/a[2]").click()
driver.find_element(by=By.LINK_TEXT, value="收货地址").click()
num = driver.find_element(by=By.XPATH, value="/html/body/div[3]/div/div[2]/div[2]/div/p/em[1]").text
print( num)
# WebDriverWait(driver, 10).until(lambda x: x.find_element((By.CSS_SELECTOR, ".co_blue")))
driver.find_element(by=By.XPATH, value="//*[text()='增加新地址']").click()
WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.XPATH, value="/html/body/div[4]/form/div[2]/div/div[2]/div[1]/div/input"))
driver.find_element(by=By.XPATH, value="/html/body/div[4]/form/div[2]/div/div[2]/div[1]/div/input").send_keys("张三")
driver.find_element(by=By.XPATH, value="/html/body/div[4]/form/div[2]/div/div[2]/div[2]/div/input").send_keys("18000000000")
# 下拉框
Select(driver.find_element(by=By.ID, value="province")).select_by_visible_text("甘肃省")
time.sleep(3)
Select(driver.find_element(by=By.ID, value="city")).select_by_visible_text("兰州市")
time.sleep(3)
Select(driver.find_element(by=By.ID, value="district")).select_by_visible_text("榆中县")
time.sleep(3)
Select(driver.find_element(by=By.ID, value="twon")).select_by_visible_text("夏官营乡（镇）")
driver.find_element(by=By.XPATH, value="/html/body/div[4]/form/div[2]/div/div[2]/div[4]/div/input").send_keys("雄安米西奥小区十一单元66楼")
driver.find_element(by=By.XPATH, value="/html/body/div[4]/form/div[2]/div/div[2]/div[5]/div/input").send_keys("555555")
driver.find_element(by=By.ID, value="address_submit").click()
time.sleep(3)
# driver.refresh()
num1 = driver.find_element(by=By.XPATH, value="/html/body/div[3]/div/div[2]/div[2]/div/p/em[1]").text
print(num1)
assert int(num1) == int(num)+1
# 5.暂停
time.sleep(3)
# .关闭浏览器驱动对象
driver.quit()
